package com.open.party.cloud.view.home.article.fragment

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import cn.android.x.inters.AndroidView
import cn.android.x.model.data.AppPageData
import cn.android.x.model.data.PageReq
import cn.android.x.model.data.ResultInfo
import cn.android.x.parent.AppRecycleViewBaseFragment
import com.open.party.cloud.R
import com.open.party.cloud.model.ArticleEntity
import com.open.party.cloud.model.ArticleVo
import com.open.party.cloud.model.DictionaryBean
import com.open.party.cloud.view.home.article.ArticleDetailsActivity
import com.open.party.cloud.view.home.article.adapter.ArticleListAdapter
import com.open.party.cloud.viewModel.ArticlePresent
import com.sinothk.android.utils.XUtils
import com.sinothk.android.utils.inters.OnSuperListener
import com.sinothk.rxretrofit.bean.PageVo
import kotlinx.android.synthetic.main.comm_fragment_rv_loading_list.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode


class ArticleListFragment : AppRecycleViewBaseFragment<ArticleEntity>(), OnSuperListener<ArticleEntity>, AndroidView<ResultInfo<AppPageData<ArticleEntity>>> {

    private var viewModel: ArticlePresent? = null
    private var adapter: ArticleListAdapter? = null
    private var currView: View? = null
    private var type: String? = null

    override fun onAttach(context: Context) {
        super.onAttach(context)
        type = requireArguments().getString("type")
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        if (currView == null) {
            currView = inflater.inflate(R.layout.comm_fragment_rv_loading_list, container, false)
        }
        return currView
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        viewModel = ArticlePresent(this)

        initRecycleLinearView(loadingRecyclerView)

        adapter = ArticleListAdapter(activity)
        loadingRecyclerView.adapter = adapter
        adapter?.setOnSuperListener(this)

        loadingTipView.setRetryListener {
            loadingTipView.showLoading("努力加载中")
            refreshData()
        }

        loadingTipView.showLoading("努力加载中")
        refreshData()
    }

    override fun loadData(pageVo: PageVo<ArticleEntity>) {
        val pageReq = PageReq<ArticleVo>()
        pageReq.pageNo = pageVo.pageNum
        pageReq.pageSize = pageVo.pageSize
        val vo = ArticleVo()
        vo.childrenType = DictionaryBean(type, "")
        pageReq.data = vo

        viewModel!!.getArticleListData(pageReq)
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun eventBusCallback(result: ResultInfo<AppPageData<ArticleEntity>>) {
    }

    private fun setPageData(appPageData: AppPageData<ArticleEntity>) {
        if (loadType == LoadType.REFRESH) {
            if (appPageData.records.size == 0) {
                loadingTipView.showEmpty("暂无数据", R.drawable.empty_nodata)
                loadingRecyclerView.setLoadingMoreEnabled(false)
            } else {
                adapter!!.setData(appPageData.records!!)
                loadingTipView?.showContent()
            }
        } else {
            if (appPageData.records.size == 0) {
                tip("没有更多数据了")
            } else {
                adapter!!.updateData(appPageData.records!!)
                loadingTipView?.showContent()
            }
        }

        // 设置：通用
        if (appPageData.current < appPageData.pages) {
            loadingRecyclerView.setLoadingMoreEnabled(true)
        } else {
            loadingRecyclerView.setLoadingMoreEnabled(false)
            loadingRecyclerView.setNoMore(true)
        }
        stopLoading(loadingRecyclerView, loadType)
    }

    override fun onClick(position: Int, itemData: ArticleEntity, flag: String?) {
        if (!itemData.isExternalLink) {
            if (XUtils.string().isEmpty(itemData.id)){
                XUtils.toast().show("内部资料，id不能为空")
                return
            }
            XUtils.intent().openActivity(activity, ArticleDetailsActivity::class.java)
                .putStringExtra("id", itemData.id)
                .startInFragment(this)
        } else {
            if (XUtils.string().isEmpty(itemData.source)){
                XUtils.toast().show("外部资料，source不能为空")
                return
            }

            val intent = Intent()
            intent.action = "android.intent.action.VIEW"
            val contentUrl: Uri = Uri.parse(itemData.source)
            intent.data = contentUrl
            startActivity(intent)
        }
    }

    override fun loadingDismiss() {
    }

    override fun complete(result: ResultInfo<AppPageData<ArticleEntity>>) {
        when (result.code) {
            ResultInfo.SUCCESS -> {
                if (result.data != null) {
                    setPageData(result.data)
                } else {
                    loadingTipView.showError("数据异常")
                }
            }
            ResultInfo.TOKEN_OVERDUE -> {
                loadingTipView.showError("Token过期，请重新登录")
            }
            else -> {
                loadingTipView?.showError(result.msg)
            }
        }
    }

    override fun loadingShow() {
    }

    override fun showTip(msg: String?) {
    }
}